#include <stdio.h>
#include <string.h>
#include "jy61p.h"

static char RxBuffer[11];/*接收数据数组*/
static volatile uint8_t RxState = 0;/*接收状态标志位*/
static uint8_t RxIndex = 0;/*接受数组索引*/
float Roll,Pitch,Yaw;/*角度信息，如果只需要整数可以改为整数类型*/
float Gyro_X,Gyro_Y,Gyro_Z;
float Acc_X,Acc_Y,Acc_Z;
short X,Y;
struct SGyro stcGyro;

struct SAcc stcAcc;
struct SQs stcQs;

/**
 * @brief       数据包处理函数
 * @param       串口接收的数据RxData
 * @retval      无
 */
void jy61p_ReceiveData(uint8_t RxData)
{
	uint8_t i,sum=0;
	
	if (RxState == 0)	//等待包头
	{
		if (RxData == 0x55)	//收到包头
		{
			RxBuffer[RxIndex] = RxData;
			RxState = 1;
			RxIndex = 1; //进入下一状态
		}
	}
	
	else if (RxState == 1)
	{
		if (RxData == 0x53||RxData==0x51||RxData==0x59)	/*判断数据内容，修改这里可以改变要读的数据内容，0x53为角度输出*/
		{
			RxBuffer[RxIndex] = RxData;
			RxState = 2;
			RxIndex = 2; //进入下一状态
		}
	}
	
	else if (RxState == 2)	//接收数据
	{
		RxBuffer[RxIndex++] = RxData;
		if(RxIndex == 11)	//接收完成
		{
			for(i=0;i<10;i++)
			{
				sum = sum + RxBuffer[i]; //计算校验和
			}
			if(RxBuffer[1]==0x53&&sum == RxBuffer[10])		//校验成功
			{
				/*计算数据，根据数据内容选择对应的计算公式*/
				Roll = ((uint16_t) ((uint16_t) RxBuffer[3] << 8 | (uint16_t) RxBuffer[2])) / 32768.0f * 180.0f;
				Pitch = ((uint16_t) ((uint16_t) RxBuffer[5] << 8 | (uint16_t) RxBuffer[4])) / 32768.0f * 180.0f;
				Yaw = ((uint16_t) ((uint16_t) RxBuffer[7] << 8 | (uint16_t) RxBuffer[6])) / 32768.0f * 180.0f;
                //printf("%.2f\n",Yaw);
               // printf("Roll: %.2f, Pitch: %.2f, Yaw: %.2f\n", Roll, Pitch, Yaw);

            }
            else if(RxBuffer[1]==0x51&&sum == RxBuffer[10]){
                memcpy(&stcAcc,&RxBuffer[2],8);
                //printf("x:%.2f,y:%.2f,z:%.2f\n",(float)stcAcc.a[0]/32768.0*16.0,(float)stcAcc.a[1]/32768.0*16.0,(float)stcAcc.a[2]/32768.0*16.0);

            }

			RxState = 0;
			RxIndex = 0; //读取完成，回到最初状态，等待包头
		}
	}
}

